package com.gs.study.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/** 
 * @author gaoshang 
 * @date 创建时间：2018年5月7日 下午5:37:08 
 * @version 1.0 
 * @parameter  
 * @since  
 * @return  
 */
public class BaseController implements MessageSourceAware {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;
	
	@ModelAttribute
	public void setServletRequest(HttpServletRequest request){
		this.request = request;
		try {
			request.setCharacterEncoding("utf-8");
			session = request.getSession();
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage());
		}
	}
	
	@ModelAttribute
	public void setResponse(HttpServletResponse response) {
		this.response = response;
		response.setCharacterEncoding("utf-8");	
	}
	
	/**
	 * 返回（输出）字符串到前台
	 * @param result
	 */
	protected void printString(String result){
		try {
			//设置编码(调用setContentType或者setCharacterEncoding)
			response.setContentType("text/html;charset=utf-8"); 
			PrintWriter out = response.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			logger.error(e.getMessage());		
		}
	}

	@Autowired
	private MessageSource messageSource;
	 /* 获取国际化信息
	 * @param messageCode
	 * @param params
	 * @param defaultValue
	 * @return
	 */
	protected String getMessage(String messageCode, Object[] params,
			String defaultValue) {
		return (messageSource.getMessage(messageCode, params, defaultValue,
				((ServletRequestAttributes) RequestContextHolder
						.getRequestAttributes()).getRequest().getLocale()));
	}
	
	/**
	 * 国际化的封装
	 * @param code
	 * @return
	 */
	protected String getInternalValue(String code) {
		return this.getMessage(code, null, "");
	}

	
	
	/**
     * 计算两个时间相差的天数
     * @param smdate 较小的时间 
     * @param bdate 较大的时间 
     */
	public int daysBetween(Timestamp smdate,Timestamp bdate){    
        long time1 = smdate.getTime();                 
        long time2 = bdate.getTime();         
        long between_days=(time2-time1)/(1000*3600*24);  
        return Integer.parseInt(String.valueOf(between_days));           
    }

	@Override
	public void setMessageSource(MessageSource messageSource) {
		// TODO Auto-generated method stub
		
	}
}
